﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;
using ReporterEngine;

namespace ReporterControlLibrary
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class InstanceTree : UserControl
    {
        public Brush TreeBackground
        {
            get { return (Brush)this.GetValue(TreeBackgroundProperty); }
            set { this.SetValue(TreeBackgroundProperty, value); }
        }
        public static readonly DependencyProperty TreeBackgroundProperty = DependencyProperty.Register(
          "TreeBackground", typeof(Brush), typeof(InstanceTree), new PropertyMetadata(Brushes.Transparent));

        public InstanceTree()
        {
            this.InitializeComponent();
            //this.Loaded += new RoutedEventHandler(InstanceTree_Loaded);
           // InstancesTree.AddHandler(TreeViewItem.ExpandedEvent, new RoutedEventHandler(TreeViewItemExpanded));
        }

        private CIFModel cifModel;

        void InstanceTree_Loaded(object sender, RoutedEventArgs e)
        {
            var ee = this.DataContext;
            //var content = File.ReadAllText(System.IO.Path.Combine(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Provision Reports"), "report template 1\\Export\\cif.xml"));

            //var cif = XDocument.Parse(content);

            //cifModel = new CIFModel();

            //cifModel.Parse(cif);

            //CollectionViewSource icvs = this.TryFindResource("InstanceNodesModelDataSource") as CollectionViewSource;
            //if (icvs != null)
            //{
            //    icvs.Source = cifModel.GetInstanceCategories();
            //}
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //sQuery query = sQuery.New(cifModel);
            //query.Elements.Add(cifModel.Notebook);

            //TextRange documentRange = new TextRange(PathBox.Document.ContentStart, PathBox.Document.ContentEnd);

            //var actualPath = LexPath.Scan(documentRange.Text);

            //Interpreter target = Interpreter.Create(actualPath); // TODO: Initialize to an appropriate value

            //var actual = target.Execute(actualPath, query);

            //CollectionViewSource icvs = this.TryFindResource("InstanceNodesModelDataSource") as CollectionViewSource;
            //var tres = cifModel.GetInstanceCategories(actual.Elements);
            //icvs.Source = tres;
        }

        //private void TreeViewItemExpanded(object sender, RoutedEventArgs e)
        //{
        //    AppData.ExpandNode(((TreeViewItem)e.OriginalSource).Header);
        //}
    }
}
